package com.mavenq.fly.test;

import com.alibaba.fastjson.JSONObject;
import com.mavenq.fly.utils.TokenUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import java.net.URI;
import java.util.Calendar;
import java.util.Map;

/**
 * 企业接口示例代码
 */
public class EnterpriseDemo {

    public static void main(String[] args) throws Exception {
        //接口地址
        String restSever = "https://92.1.18.12/id-base/v5/openapi/enterprise";

        // 应用标识
        String appKey = "36df0597ce964b39814110e594ddb321";
        // 身份系统签发给应用对接的密钥
        String pwd = "ff2874c323d24b2890589436e00faa81";
        // 时间戳
        Long ts = Calendar.getInstance().getTime().getTime();
        // 随机数
        String once = RandomStringUtils.randomAlphanumeric(32);
        String signMethod = "SHA-256";

        JSONObject obj = new JSONObject();
        //请求参数
        obj.put("orgName", "腾讯");
        obj.put("userName", "xiaoyangge");
        obj.put("idCardNo", "110101199001017292");
        String bodyData = obj.toString();

        String commonParamUrl = String.format("appKey=%s" + "&" + "bodyData=%s" + "&" + "ts=%s" + "&" + "once=%s" + "&" + "signMethod=%s", appKey, bodyData, ts, once, signMethod);

        HttpPost httpPost = new HttpPost(restSever);
        System.out.println("请求参数：" + commonParamUrl);
        // 签名数据
        String signData = TokenUtils.getSignature(pwd, commonParamUrl);
        // 添加header参数 appCode、timestamp、 signatureNonce、signature
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        httpPost.addHeader("appKey", appKey);
        httpPost.addHeader("ts", ts.toString());
        httpPost.addHeader("once", once);
        httpPost.addHeader("signMethod", signMethod);
        httpPost.addHeader("signData", signData);

        httpPost.setEntity(new StringEntity(bodyData));
        String urlStr = httpPost.getURI().toString();

        if (StringUtils.endsWith(urlStr, "/")) {
            urlStr = StringUtils.removeEnd(urlStr, "/");
        }

        httpPost.setURI(new URI(urlStr));
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();
        httpPost.setConfig(requestConfig);

        // 创建HttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 执行请求
        CloseableHttpResponse response = httpclient.execute(httpPost);
        // 取响应的结果
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();

        Map<String, Object> parseObject  = JSONObject.parseObject(EntityUtils.toString(entity), Map.class);
        System.out.println("r:" + parseObject);
        //todo 对返回值进行处理
        String code = parseObject.get("code").toString();
        System.out.println("code:" + code);
        if(!"0".equals(code)) {
            System.out.println("error:" + parseObject.get("message").toString());
        }
        Object records = parseObject.get("result");
    }
}
